package com.example.dangdangbackend.controller;

import com.example.dangdangbackend.DTO.AddCategoryDTO;
import com.example.dangdangbackend.VO.CategoryFirstVO;
import com.example.dangdangbackend.VO.CategorySecondVO;
import com.example.dangdangbackend.VO.CategoryVO;
import com.example.dangdangbackend.entity.CategoryFirst;
import com.example.dangdangbackend.entity.Result;
import com.example.dangdangbackend.service.CategoryService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 获取所欲的标题，以树的结构
     * @return
     */
    @PostMapping("/getAll")
    public Result<List<CategoryFirst>> getAll(){
        List<CategoryFirst> category = categoryService.selectAllCategory();
        if(category.isEmpty()){
            return Result.error("为空");
        }

        return Result.success(category);
    }

    /**
     * 添加一级标题
     * @param categoryFirstName
     * @return
     */
    @PostMapping("/addFirst")
    public Result<String> addFirst(String categoryFirstName){
        categoryService.addCategoryFirst(categoryFirstName);

        return Result.success("新添成功");
    }

    /**
     * 添加二级标题
     * @param addCategoryDTO
     * @return
     */
    @PostMapping("/addSecond")
    public Result<String> addSecond(AddCategoryDTO addCategoryDTO){
        categoryService.addCategorySecond(addCategoryDTO);
        return Result.success("新增成功");
    }

    /**
     * 获取所有的一级标题
     * @return
     */
    @PostMapping("/selectAllFirst")
    public Result<List<CategoryFirstVO>> selectAllFirst(){
        List<CategoryFirstVO> vos = categoryService.selectAllFirst();

        return Result.success(vos);
    }

    /**
     * 获取所有的二级标题
     * @return
     */
    @PostMapping("/selectAllSecond")
    public Result<List<CategorySecondVO>> selectAllSecond(){
        List<CategorySecondVO> vos = categoryService.selectAllSecond();

        return Result.success(vos);
    }

    /**
     * 分页查询出所有的分类
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/selectByPage")
    public Result<PageInfo<CategoryVO>> selectByPage(int pageNum, int pageSize){
        PageInfo<CategoryVO> pageInfo = categoryService.selectByPage(pageNum, pageSize);
        return Result.success(pageInfo);
    }

    @PostMapping("/deleteCategory")
    public Result<String> deleteCategory(int categoryId,int categoryLevel){
        if(categoryLevel==1){
            categoryService.deleteFirst(categoryId);
        }else if (categoryLevel==2){
            categoryService.deleteSecond(categoryId);
        }else {
            return Result.error("删除异常");
        }

        return Result.success("删除分类成功");
    }
}
